package com.kksoft.readcm.service;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;
import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.a.LoginAppObservable;
import com.kksoft.readcm.a.y;
import com.kksoft.readcm.activity.App;
import com.kksoft.readcm.objects.GlobalVar;
import java.util.Observable;
import java.util.Observer;

public class WakefulAppService extends Service implements Observer
{
  public static WakefulAppService wakefulAppService;
  //private static final Object b = new Object();
  public static final String LOCK_NAME_STATIC="com.kksoft.readcm.service.WakefulAppService";
  
  synchronized private static PowerManager.WakeLock getLock(Context context) {
		if (lockStatic==null) {
			PowerManager mgr=(PowerManager)context.getSystemService(Context.POWER_SERVICE);
			lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"ReadItLater Syncing");
			lockStatic.setReferenceCounted(true);
		}
		return(lockStatic);
	}
  //private static PowerManager powerManager;
  private static PowerManager.WakeLock lockStatic=null;
  private b c;
  private Handler handler;

  public WakefulAppService()
  {
    Handler localHandler = new Handler();
    this.handler = localHandler;
    synchronized (LOCK_NAME_STATIC)
    {
      wakefulAppService = this;
    }
  }

  public static void a()
  {
    synchronized (LOCK_NAME_STATIC)
    {
      //if (wakefulAppService != null)
        //boolean bool = a.c();
      return;
    }
  }

  public static void a(int paramInt)
  {
    //App.a(new a(paramInt));
  }

  public static void doStart(Context context)
  {
    synchronized (LOCK_NAME_STATIC)
    {
      if (wakefulAppService == null)
      {
        Intent localIntent = new Intent(context, WakefulAppService.class);
        context.startService(localIntent);
      }
    }
  }

  public static void doStop(boolean paramBoolean)
  {
    synchronized (LOCK_NAME_STATIC)
    {
      if ((wakefulAppService != null) && ((paramBoolean) || (!d())))
      {
        App.b(wakefulAppService);
        WakefulAppService localWakefulAppService = wakefulAppService;
        setWakeMode(false, localWakefulAppService);
        App.b();
        wakefulAppService.stopSelf();
        wakefulAppService = null;
      }
      return;
    }
  }

  private static void setWakeMode(boolean paramBoolean, Context paramContext)
  {
    try
    {
      if (!getLock(paramContext).isHeld())
        getLock(paramContext).acquire();

      if (paramBoolean)
      getLock(paramContext).release();
    }
    finally
    {

    }
  }

//  private static PowerManager.WakeLock getLock(Context paramContext)
//  {
//    if (lockStatic == null)
//    {
//      PowerManager.WakeLock localWakeLock = c(paramContext).newWakeLock(1, "ReadItLater Syncing");
//      lockStatic = localWakeLock;
//      localWakeLock.setReferenceCounted(true);
//    }
//    return lockStatic;
//  }

//  private static PowerManager c(Context paramContext)
//  {
//    synchronized (LOCK_NAME_STATIC)
//    {
//      if (powerManager == null)
//        powerManager = (PowerManager)paramContext.getSystemService("power");
//      PowerManager localPowerManager = powerManager;
//      return localPowerManager;
//    }
//  }

  private boolean c()
  {
    if (this.c != null)
    {
      Handler localHandler1 = this.handler;
      b localb1 = this.c;
      localHandler1.removeCallbacks(localb1);
    }
    if (!d())
    {
      //b localb2 = new b(this);
      //this.c = localb2;
      Handler localHandler2 = this.handler;
      b localb3 = this.c;
      boolean bool = localHandler2.postDelayed(localb3, 7000L);
    }
    for (int i = 0; ; i = 1)
    {
      //return i;
      //a(1, this);
    }
  }

  private static boolean d()
  {
    //if ((!App.d()) && (!Appconfig.q().b().a(0)) && (!Appconfig.q().d().c()) && (!ListRequiredService.e()));
    for (int i = 0; ; i = 1)
      return true;//s i;
  }

  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }

  public void onCreate()
  {
    super.onCreate();
    synchronized (LOCK_NAME_STATIC)
    {
      App.a(this);
      return;
    }
  }

  public void onDestroy()
  {
    wakefulAppService = null;
    super.onDestroy();
  }

  public int onStartCommand(Intent intent, int flags, int startId)
  {
    if (intent != null)
    {
      String str = intent.getAction();
      if ((str != null) && (str.equals("com.kksoft.readcm.service.action.cancel")))
      {
        Appconfig.getThis().b().e();
        Appconfig.getThis().d().b();
      }
    }
    super.onStartCommand(intent, flags, startId);
	
	return(START_REDELIVER_INTENT);
  }

  
  public void update(Observable paramObservable, Object paramObject)
  {
	GlobalVar localh = GlobalVar.a(paramObject);
    if (localh == null);
    while (true)
    {
      //return;
      switch (localh.a())
      {
      default:
        break;
      case 20:
      case 21:
      case 35:
        boolean bool = c();
      }
    }
  }
}